((window, undefined) => {
  class Food {
    constructor(option) {
      // 检测传入的是否为对象
      option = option instanceof Object ? option : {};
      this.width = option.width || 20;
      this.height = option.height || 20;
      this.color = option.color || "green";
      this.x = option.x || 0;
      this.y = option.y || 0;
      // 存储对象创建出来的div元素
      this.elements = [];
    }
    // 将食物渲染到页面中
    render (map) {
      // 创建元素
      this.ele = document.createElement("div");
      // 随机获取x y 
      this.x = Tools.getRandom(0, map.clientWidth / this.width - 1) * this.width;
      this.y = Tools.getRandom(0, map.clientHeight / this.height - 1) * this.height;
      // 添加对应的样式
      this.ele.className = 'food';
      this.ele.style.width = this.width + 'px';
      this.ele.style.height = this.height + 'px';
      this.ele.style.top = this.y + 'px';
      this.ele.style.left = this.x + 'px';
      this.ele.style.backgroundColor = this.color;
      // 添加到父级中
      map.appendChild(this.ele);
      // 方便后期删除
      this.elements.push(this.ele);
    }
    // 删除食物的方法
    remove (map, i) {
      // 通过一些方法获取要被删除食物的下标
      // 将元素从html中删除
      map.removeChild(this.elements[i]);
      // 将元素从数组删除
      this.elements.splice(i, 1);
    }
  }

  window.Food = Food;
})(window);